package yx.action;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;

import yx.service.UserServiceInt;
import yx.vo.JsonVO;
import yx.vo.UserVO;

import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("basePackage")
@Namespace("/")
@Action(value = "loginAction")
public class LoginAction extends BaseAction implements ModelDriven<UserVO> {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(LoginAction.class);

	private UserVO user = new UserVO();

	@Override
	public UserVO getModel() {
		return user;
	}

	private UserServiceInt userService;

	public UserServiceInt getUserService() {
		return userService;
	}

	@Autowired
	public void setUserService(UserServiceInt userService) {
		this.userService = userService;
	}

	/**
	 * 用户登录
	 */
	public String login() {
		JsonVO j = new JsonVO();
		UserVO u = userService.login(user);
		if (u != null) {
			j.setSuccess(true);
			HttpSession session = ServletActionContext.getRequest().getSession();
			session.setAttribute("LOGIN_USER", u);
			j.setMsg("登录成功！");
		} else {
			j.setMsg("用户名或密码不正确！");
		}
		super.writeJson(j);
		return null;
	}

	/**
	 * 用户退出系统
	 */
	public String logout() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.removeAttribute("LOGIN_USER");

		JsonVO j = new JsonVO();
		j.setMsg("退出成功！");
		super.writeJson(j);
		return null;
	}

}
